package com.wenc.psi.dao;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.wenc.psi.pojo.Part;
import com.wenc.psi.utils.JDBCUtils;
import com.wenc.psi.pojo.Employee;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDao extends JDBCUtils implements Dao<Employee>{

    @Override
    public void insert(Employee... employees) {

    }

    @Override
    public void delete(Employee... employees) {

    }

    @Override
    public void delete(String... ids) {

    }

    @Override
    public void update(Employee oldT, Employee newT) {

    }

    @Override
    public List<Employee> getAllData() {

        return null;
    }

    @Override
    public Employee getDataById(String id) {
        Employee employee;
        try {
            conn = getConnection();
            String sql = "SELECT * FROM employee where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                employee = new Employee(rs.getString("id"),
                        rs.getString("name"),
                        rs.getString("pinyin"),
                        rs.getString("photourl"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("position"),
                        rs.getString("phone"),
                        rs.getString("email"),
                        rs.getString("qq"),
                        rs.getString("wechat"),
                        rs.getString("userid"),
                        rs.getString("password")
                        );
                return employee;
            } else return null;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,stmt,conn);
        }
        return null;
    }

    @Override
    public LiveData<List<Employee>> getAllLiveData() {
        return null;
    }

    // 登录
    public Employee login(String userid, String password){
        Employee employee;
        try {
            //1.获取连接
            conn = JDBCUtils.getConnection();
            System.out.println("获取连接成功");
            //2.定义sql
            String sql = "select * from Employee where userid = ? and password = ? ";
            //3.获取执行sql对象
            pstmt = conn.prepareStatement(sql);
            //4.给?赋值
            pstmt.setString(1,userid);
            pstmt.setString(2,password);
            //5.执行查询,不需要传递sql
            rs = pstmt.executeQuery();
            //6.判断
            if (rs.next()) {
                employee = new Employee(rs.getString("id"),
                        rs.getString("name"),
                        rs.getString("pinyin"),
                        rs.getString("photourl"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("position"),
                        rs.getString("phone"),
                        rs.getString("email"),
                        rs.getString("qq"),
                        rs.getString("wechat"),
                        rs.getString("userid"),
                        rs.getString("password")
                );
                return employee;
            } else return null;
        } catch (SQLException e) {
            System.out.println("sql执行失败");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, pstmt, conn);
        }
        return null;
    }

}
